package com.loan.psychicloan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.MyCountDownTimer;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.ValidateUtil;
import com.loan.psychicloan.bean.SendMSMBean;
import com.orhanobut.logger.Logger;

/**
 * Created by watilion on 2018/1/5 0005.
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    private boolean is_check = true;
    EditText register_username;
    EditText register_msg;
    EditText register_psw;
    Button btn_register_sendsms;
    Button btn_register;
    CheckBox cb_register_xy;
    TextView tv_register_xy;
    TextView tv_2login;
    private SendMSMBean mSendMSMBean = new SendMSMBean();
    private SendMSMBean validataNumBean =new SendMSMBean();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar mToolbar = findViewById(R.id.register_toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView tv_back_toolbar_title = findViewById(R.id.back_toolbar_title);
        tv_back_toolbar_title.setText("注册");

        initview();
    }

    private void initview() {
        register_username = findViewById(R.id.register_username);
        register_msg = findViewById(R.id.register_msg);
        register_psw = findViewById(R.id.register_psw);
        btn_register_sendsms = findViewById(R.id.btn_register_sendsms);
        btn_register = findViewById(R.id.btn_register);
        cb_register_xy = findViewById(R.id.cb_register_xy);
        tv_register_xy = findViewById(R.id.tv_register_xy);
        tv_2login = findViewById(R.id.tv_2login);

        btn_register_sendsms.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        cb_register_xy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                is_check = cb_register_xy.isChecked();
                Toast.makeText(getApplicationContext(),"checkbox状态为"+is_check,Toast.LENGTH_SHORT).show();
            }
        });
        tv_register_xy.setOnClickListener(this);
        tv_2login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btn_register_sendsms:

                String Phonenumber = register_username.getText().toString();
                if (!ValidateUtil.validatePhoneNumber(Phonenumber)){
                    Toast.makeText(this,"手机号不正确，请检查手机号",Toast.LENGTH_SHORT).show();
                }else {
                    JsonObject jsonObject = new JsonObject();
                    //jsonObject.notifyAll();
                    //jsonObject.remove("");
                    jsonObject.addProperty("phonenumber", Phonenumber);
                    jsonObject.addProperty("type","0");
                    startSendMSMTask(LoginActivity.sendmsm_url,jsonObject.toString());

                }
                break;
            case R.id.btn_register:
                if (is_check){
                        if (register_msg.getText() == null) {
                            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                        } else {
                            if (register_psw == null) {
                                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                            } else if (!ValidateUtil.validatePass(register_psw.getText().toString())) {
                                Toast.makeText(this, "密码不符合要求，请检查密码长度（6-12位）", Toast.LENGTH_SHORT).show();
                            } else {
                                JsonObject mRegister_JsonObject = new JsonObject();
                                mRegister_JsonObject.addProperty("phonenumber", register_username.getText().toString());
                                mRegister_JsonObject.addProperty("type", "0");
                                mRegister_JsonObject.addProperty("tamp", mSendMSMBean.data.tamp);
                                mRegister_JsonObject.addProperty("hashcode", mSendMSMBean.data.hashcode);
                                mRegister_JsonObject.addProperty("code", register_msg.getText().toString());
                                mRegister_JsonObject.addProperty("password", register_psw.getText().toString());
                                mRegister_JsonObject.addProperty("channel", "android");
                                mRegister_JsonObject.addProperty("platform", "android");
                                Logger.json(mRegister_JsonObject.toString());
                                startRegisterTask(LoginActivity.validataNumUrl, mRegister_JsonObject.toString());

                                //Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
                            }
                        }
                }else{
                    Toast.makeText(getApplicationContext(),"请认真阅读《超能贷注册协议》后，再进行注册",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_register_xy:
                Intent mIntent = new Intent(getApplicationContext(),WebViewActivity.class);
                mIntent.putExtra("loanurl","https://www.psychicloan.com:8445/cnd/xieyi.html");
                startActivity(mIntent);
                break;
            case R.id.tv_2login:
                mIntent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(mIntent);
                finish();
        }
    }
    private void startRegisterTask(String validataNumUrl, String json) {

        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, validataNumUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            validataNumBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),validataNumBean.msg,Toast.LENGTH_SHORT).show();
                            saveRegister(validataNumBean);
                        }
                    }
                });
    }

    private void saveRegister(SendMSMBean mValidataNumBean) {
        if (mValidataNumBean.resultcode ==8){
            MainActivity.LOGIN = true;
            MainActivity.PHONE_NUMBER = register_username.getText().toString();
            SharedPreferences sp = getSharedPreferences("config",0);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("PHONE_NUMBER",register_username.getText().toString());
            editor.putBoolean("LOGIN",true);
            editor.apply();
            Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(mIntent);
            finish();

        }
    }
    public void startSendMSMTask(String mUrl, String json) {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, mUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            mSendMSMBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),mSendMSMBean.msg,Toast.LENGTH_SHORT).show();

                            setCountDownTimer(mSendMSMBean);
                        }
                    }
                });
    }
    public void setCountDownTimer(SendMSMBean mSendMSMBean) {
        if (mSendMSMBean.resultcode == 2){
            //Toast.makeText(getApplicationContext(),"tamp"+mSendMSMBean.data.tamp,Toast.LENGTH_SHORT).show();
            final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(btn_register_sendsms,60000,1000);
            myCountDownTimer.start();
        }
    }
}
